package com.example.demo_springboot;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;

@SpringBootApplication
//@EnableCaching //开启缓存
//@EnableScheduling //开启定时任务
public class DemoSpringbootApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(DemoSpringbootApplication.class);
    public static void main(String[] args) throws InterruptedException {
        //启动web 返回上下文对象
        ApplicationContext  ctx = SpringApplication.run(DemoSpringbootApplication.class, args);

//        redis 发布/订阅模式测试
//        从启动服务中获取启动中已经注入的bean ,
//        StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
//        CountDownLatch latch = ctx.getBean(CountDownLatch.class);
//        LOGGER.info("Sending message...");
//        获取到的bean调用方法
//        template.convertAndSend("chat", "Hello from Redis!");
//        延迟
//        latch.await();
//        System.exit(0);
    }

    //springboot启动是加载注入的bean , 启动后马上运行,这种方式和实现CommandLineRunner接口有类似效果
    //这里没有@Order注解 , 顺序应该在AppRunner 之后才会输出
    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            System.out.println("获取已经注入Spring Boot中的bean列表:");
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
//                System.out.println(beanName);
            }

        };
    }
}
